package org.example.algorithm.dp;

public class MaxProfitHasFee {

    //dp[i][1] = max(dp[i-1][1],dp[i-1][0]-prices[i])
    //dp[i][0] = max(dp[i-1][0],dp[i-1][1]+prices[i]-fee)
    public int maxProfit(int[] prices, int fee) {
        int len = prices.length;
        if (len == 0) {
            return 0;
        }
        int[][] dp = new int[len+1][2];
        dp[0][0] = 0;
        dp[0][1] = Integer.MIN_VALUE / 2;
        for (int i=0;i<len;i++) {
            dp[i+1][1] = Math.max(dp[i][1],dp[i][0]-prices[i]);
            dp[i+1][0] = Math.max(dp[i][0],dp[i][1]+prices[i]-fee);
        }
        return dp[len][0];
    }
}
